<?php
namespace App\Servers\Backend;

use App\Repositories\Backend\SubjectRepository;

class SubjectServer extends CommonServer
{

    public function __construct(
        SubjectRepository $subjectRepository
    ) {
        $this->subjectRepository = $subjectRepository;
    }

    /**
     * 列表
     * @param  Array $input [search]
     * @return Array
     */
    public function index($input)
    {
        $result['lists']   = $this->subjectRepository->getLists($input);

        return ['获取成功', $result];
    }

    /**
     * 新增
     * @param  Array $input [subject_name, comment, order, status, subject_image1 , subject_image2]
     * @return Array
     */
    public function create($input)
    {
        $subject_name      = isset($input['subject_name']) ? strval($input['subject_name']) : '';
        $comment     = isset($input['comment']) ? strval($input['comment']) : '';
        $order    = isset($input['order']) ? intval($input['order']) : 0;
        $status        = isset($input['status']) ? intval($input['status']) : 0;
        $subject_image1         = isset($input['subject_image1']) ? strval($input['subject_image1']) : '';
        $subject_image2         = isset($input['subject_image2']) ? strval($input['subject_image2']) : '';


        if (!$subject_name || !$comment || !$order || !$status || !$subject_image1 || !$subject_image2) {
            return ['code' => ['x00004', 'system']];
        }

        $result['list'] = $this->subjectRepository->create($subject_name, $comment, $order, $status , $subject_image1 , $subject_image2);

        if (!$result['list']) {
            return ['code' => ['x00001', 'system']];
        }

        return ['新增成功', $result];
    }






    /**
     * 编辑
     * @param  Array $input [subject_name, comment, order, status, subject_image1 , subject_image2]
     * @param  Int $id
     * @return Array
     */
    public function update($id, $input)
    {
        $subject_name      = isset($input['subject_name']) ? strval($input['subject_name']) : '';
        $comment     = isset($input['comment']) ? strval($input['comment']) : '';
        $order    = isset($input['order']) ? intval($input['order']) : 0;
        $status        = isset($input['status']) ? intval($input['status']) : 0;
        $subject_image1         = isset($input['subject_image1']) ? strval($input['subject_image1']) : '';
        $subject_image2         = isset($input['subject_image2']) ? strval($input['subject_image2']) : '';

        if (!$id ||!$subject_name || !$comment || !$order || !$status || !$subject_image1 || !$subject_image2) {
            return ['code' => ['x00004', 'system']];
        }

        $result = $this->subjectRepository->update($id, $subject_name, $comment, $order, $status , $subject_image1 , $subject_image2);

        if (!$result) {
            return ['code' => ['x00001', 'system']];
        }
        return ['更新成功', $result];
    }


//
//    /**
//     * 删除
//     * @param  Int $id
//     * @return Array
//     */
//    public function destroy($id)
//    {
//        $result = $this->adminRepository->destroy($id);
//
//        if (!$result) {
//            return ['code' => ['x00002', 'system']];
//        }
//
//        return ['删除成功', $result];
//    }
}
